$(function () {
    $("body").on("click",".openShareCourseBtn",function () {  //开启分销推广
        var courseId = $(this).data("id");
        var allow=$(this).data("allow")
        var temp = 1;
        if (allow == 1 || allow == 0) {
            temp = 2;
        }else{
            temp = 1;
        }
        openShareCourseFn(courseId, temp);
    })
})


/**
 * 复制课程
 * @param courseId 课程ID
 * @param callback 复制成功后的回调
 */
function copyCourse(courseId,callback) {
    $.ajax({
        url:baseloaction +'/admin/course/cou/copyCourse',
        dataType:'json',
        data:{
            'courseId':courseId
        },
        type:'post',
        success:function (result) {
            if(result.success==true){
                var _course = result.entity;
                if($.isFunction(callback)){
                    var message = '复制成功，是否去修改此课程？';
                    if(_course.courseTypeKey=='LIVE'){
                        message = '复制成功，是否去修改此直播课程？';
                    }else if(_course.courseTypeKey=='PACKAGE'){
                        message = '复制成功，是否去修改此套餐课程？';
                    }
                    parent.layer.confirm(message, {
                        icon: 3,
                        title:'提示',
                        yes: function(index){
                            parent.layer.close(index);
                            if(_course.courseTypeKey=='PACKAGE'){
                                window.location.href=baseloaction+"/admin/course/cou/initUpdatePackage?id="+_course.id;
                            }else if(_course.courseTypeKey=='LIVE'){
                                window.location.href=baseloaction+"/admin/course/cou/initLiveUpdate?id="+_course.id;
                            }else{
                                window.location.href=baseloaction+"/admin/course/cou/initUpdate?id="+_course.id;
                            }
                        },
                        cancel: function(index, layero){
                            debugger;
                            parent.layer.close(index);
                            callback();
                        }
                    });
                }
            }else{
                icModale(0,result.message);
            }
        },
        error:function (error) {
            icModale(0,'系统错误，复制课程失败');
        }
    })
}

/**
 * 推广课程开关
 * @param courseId 课程id
 * @param allow  1关闭，2开启
 */
function openShareCourseFn(courseId,allow) {
    var msg = '开启后此课程将成为分销课程参加前台各端的分销推广活动。是否开启分销课程？'
    var choseMsg = '关闭后此课程将不再成为分销课程，正在进行中的分销推广活动将不受影响。是否关闭分销课程？'
    icModale(2, allow==2?msg:choseMsg, 0, function () {
        $.ajax({
            url:baseloaction+"/admin/course/cou/updateCourseAllow",
            data:{
                courseId:courseId,
                allow:allow
            },
            dataType:"json",
            type:"post",
            success:function (res) {
                if (res.success == true) {
                    if (allow == 1 || allow == 0) {
                        layer.msg("已开启课程推广")
                    }else{
                        layer.msg("已关闭课程推广")
                    }
                    window.location.reload();
                }
            }
        })
    })
}